#!/bin/bash

echo "=========================================="
echo "  初始化数据库和测试数据"
echo "=========================================="
echo ""

# 等待 MySQL 启动
echo "⏳ 检查 MySQL 连接..."
for i in {1..10}; do
    if docker exec springcloud-mysql mysql -uroot -proot123456 -e "SELECT 1" &>/dev/null; then
        echo "✅ MySQL 已就绪"
        break
    fi
    echo "等待 MySQL 启动... ($i/10)"
    sleep 3
done

echo ""
echo "📊 创建数据库..."

# 创建数据库
docker exec springcloud-mysql mysql -uroot -proot123456 << 'SQL'
CREATE DATABASE IF NOT EXISTS seata_order CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE DATABASE IF NOT EXISTS seata_storage CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE DATABASE IF NOT EXISTS seata_account CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SQL

echo "✅ 数据库创建完成"
echo ""

# 初始化表结构和数据
# 获取脚本所在目录，然后定位到同级的 database 目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DB_DIR="$SCRIPT_DIR/../database"

# 检查 database 目录是否存在
if [ ! -d "$DB_DIR" ]; then
    echo "❌ 错误：找不到 database 目录: $DB_DIR"
    exit 1
fi

# 验证数据库是否已创建
echo "🔍 验证数据库是否已创建..."
for db in seata_order seata_storage seata_account; do
    if docker exec springcloud-mysql mysql -uroot -proot123456 -e "USE $db" &>/dev/null; then
        echo "✅ 数据库 $db 存在"
    else
        echo "❌ 警告：数据库 $db 不存在，将创建..."
        docker exec springcloud-mysql mysql -uroot -proot123456 -e "CREATE DATABASE IF NOT EXISTS $db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
    fi
done

echo ""
echo "📝 初始化 seata_order 数据库..."
if [ ! -f "$DB_DIR/01-seata_order.sql" ]; then
    echo "❌ 错误：找不到文件 $DB_DIR/01-seata_order.sql"
    exit 1
fi
# 设置字符集为 utf8mb4，确保中文注释正常显示
(echo "SET NAMES utf8mb4;"; cat "$DB_DIR/01-seata_order.sql") | docker exec -i springcloud-mysql mysql -uroot -proot123456 --default-character-set=utf8mb4 seata_order
if [ $? -eq 0 ]; then
    echo "✅ seata_order 初始化完成"
else
    echo "❌ seata_order 初始化失败"
    exit 1
fi

echo "📝 初始化 seata_storage 数据库..."
if [ ! -f "$DB_DIR/02-seata_storage.sql" ]; then
    echo "❌ 错误：找不到文件 $DB_DIR/02-seata_storage.sql"
    exit 1
fi
(echo "SET NAMES utf8mb4;"; cat "$DB_DIR/02-seata_storage.sql") | docker exec -i springcloud-mysql mysql -uroot -proot123456 --default-character-set=utf8mb4 seata_storage
if [ $? -eq 0 ]; then
    echo "✅ seata_storage 初始化完成"
else
    echo "❌ seata_storage 初始化失败"
    exit 1
fi

echo "📝 初始化 seata_account 数据库..."
if [ ! -f "$DB_DIR/03-seata_account.sql" ]; then
    echo "❌ 错误：找不到文件 $DB_DIR/03-seata_account.sql"
    exit 1
fi
(echo "SET NAMES utf8mb4;"; cat "$DB_DIR/03-seata_account.sql") | docker exec -i springcloud-mysql mysql -uroot -proot123456 --default-character-set=utf8mb4 seata_account
if [ $? -eq 0 ]; then
    echo "✅ seata_account 初始化完成"
else
    echo "❌ seata_account 初始化失败"
    exit 1
fi

echo ""
echo "📊 验证数据..."
echo ""
echo "订单表："
docker exec springcloud-mysql mysql -uroot -proot123456 seata_order \
  -e "SELECT COUNT(*) as count FROM tb_order;" 2>/dev/null | tail -n +2

echo "库存表："
docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage \
  -e "SELECT product_id, product_name, total, used, residue FROM tb_storage;" 2>/dev/null | tail -n +2

echo "账户表："
docker exec springcloud-mysql mysql -uroot -proot123456 seata_account \
  -e "SELECT user_id, user_name, total, used, residue FROM tb_account;" 2>/dev/null | tail -n +2

echo ""
echo "=========================================="
echo "  ✅ 数据库初始化完成！"
echo "=========================================="
